package com.Washer;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import com.Result.BaseRespones;
import com.Result.StatusCode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class DormitoryOperation {
	
	/**
	 * @apiDefine Dormitory 宿舍
	 */
	
	/**
	 * @api{Get} /Dormitory?method=getDormitory 查询宿舍情况
	 * @apiName getDormitory
	 * @apiGroup Dormitory
	 * @apiDescription 根据指定字段查询或获取所有宿舍信息
	 * @apiVersion 0.0.1
	 * 
	 * 
     * @apiParam {String} requestString 请求字段
     * 
	 * @apiSuccess {int} DormID 宿舍ID
	 * @apiSuccess {String} DormLocation 宿舍位置（名称）
	 * @apiSuccess {Boolean} IsOpen 是否开放 0不开放 1开放
	 * @apiSuccess {int} WasherNumber 洗衣机数量
	 * @apiSuccess {int} FreeNumber 空余数量
	 * 
	 * @apiParamExample {json} 请求样例
	 *{ 
	 *"DormID":"", 
	 *"DormLocation":"",
	 *"IsOpen":,
	 *"WasherNumber":, 
	 *"FreeNumber": 
	 *}
	 * 
	 * @apiSuccessExample {json} 返回样例
	 *{
	 *'code':3010,
	 *'msg':'宿舍信息查询成功',
	 *'timestamp':'2019-06-08 20:09:34',
	 *'data':
	 *[
	 *  {'DormID':10001,
	 *  'DormLocation':'甬江公寓四号楼',
	 *  'IsOpen':true,
	 *  'WasherNumber':5,
	 *  'FreeNumber':4},
	 *  {'DormID':10002,
	 *  'DormLocation':'甬江公寓三号楼',
	 *  'IsOpen':true,
	 *  'WasherNumber':4,
	 *  'FreeNumber':2},
	 *  {'DormID':10003,
	 *  'DormLocation':'甬江公寓二号楼',
	 *  'IsOpen':true,
	 *  'WasherNumber':5,
	 *  'FreeNumber':4},
	 *  {'DormID':10004,
	 *  'DormLocation':'本部公寓四号楼',
	 *  'IsOpen':false,
	 *  'WasherNumber':2,
	 *  'FreeNumber':1}
	 *  ]
	 *  }
	 */
	public static String getDormitory(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		
		BaseRespones response=null;
		DormitoryMapper mapper=(DormitoryMapper) session.getMapper(DormitoryMapper.class);
		System.out.println(requestString+"1");
		
		List<Dormitory> dormitories;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create(); 
		
		if(requestString.indexOf(":\"\"")>0) {
			dormitories=mapper.getAllDormitory();
		}
		//根据字段值进行筛选
		else {
			Dormitory requestDormitory=gson.fromJson(requestString, Dormitory.class);
			Map paramMap=new HashMap();
			if(requestDormitory.getDormID()!=0)
				paramMap.put("DormID",requestDormitory.getDormID());
			if(requestDormitory.getIsOpen()!=false)
				paramMap.put("IsOpen",requestDormitory.getIsOpen());
			if(requestDormitory.getDormLocation()!=null)
				paramMap.put("DormLocation",requestDormitory.getDormLocation());
			if(requestDormitory.getFreeTime()!=0)
				paramMap.put("FreeTime",requestDormitory.getFreeTime());
			if(requestDormitory.getWasherNumber()!=0)
				paramMap.put("WasherNumber",requestDormitory.getWasherNumber());
			dormitories=mapper.getDormitory(paramMap);
		}
		if(dormitories.isEmpty()) {
			response = new BaseRespones(StatusCode.DormitoryInquireFail);
			return gson.toJson(response);
		}
		response=new BaseRespones(StatusCode.DormitoryInquireSuccess);
		response.setData(dormitories);
		return gson.toJson(response);
	}
	/**
	 * @apiDefine cargoUpdateErrorMsg 添加失败响应信息
	 * @apiError 110 未知错误
	 * @apiError 111 反序列化错误
	 * @apiError 112 序列化错误
	 * @apiError 3031 宿舍信息添加失败
	 *
	 */

	/**
	 * @apiDefine cargoUpdateSuccessMsg 更新成功响应信息
	 * @apiSuccess {int} code 响应码
	 * @apiSuccess {String} msg 响应信息
	 * @apiSuccess {timestamp} timestamp 时间戳
	 */
	
	
	/**
	 * @api{Post} /Dormitory?method=insertDormitory 增加宿舍
	 * @apiName insertDormitory
	 * @apiGroup Dormitory
	 * @apiDescription 商家添加宿舍信息 在宿舍对象中给需要的字段赋值
	 * @apiVersion 0.0.1
	 * 
	 * @apiParamExample {json} 请求样例
	 *{ 
	 *"DormLocation":"本部公寓4号楼",
	 *"IsOpen":true,
	 *"WasherNumber":5, 
	 *"FreeNumber":5 
	 *}
	 * 
	 * @apiUse cargoUpdateErrorMsg
	 * @apiUse cargoUpdateSuccessMsg
	 * 
	 * @apiSuccessExample 
	 * Success-Response: 
	 * {
	 * 'code':3030,
	 * 'msg':'新增宿舍信息成功',
	 * 'timestamp':'2019-06-08 20:16:51'
	 * }
	 */
	public static String insertDormitory(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		
		BaseRespones response=null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();  
		Dormitory dormitory=null;
		
		try {
			dormitory = gson.fromJson(requestString, Dormitory.class);
		} catch (Exception e) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}

		if (dormitory==null){
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		
		
		// cargo mapper
		DormitoryMapper dormitoryMapper=(DormitoryMapper)session.getMapper(DormitoryMapper.class);
		System.out.println(gson.toJson(dormitory));
		// 执行更新
		int rs=dormitoryMapper.insertDormitory(dormitory);
		if (rs==0) {
			response = new BaseRespones(StatusCode.DormitoryInsertFail);
			return gson.toJson(response);
		}
		session.commit();
		session.close();
		response = new BaseRespones(StatusCode.DormitoryInsertSuccess);
		return gson.toJson(response);	
	}
	
	
	
}
